var classId=0;
$(document).ready( function () {
	$("#school").change(function(){
		getClass();
	});
	$("#classes").change(function(){
		getStudent();
	});
    $.ajax({
        url:"../queryAllSchool",
        type:"post",
        success:function (data) {
            if(data.code===100){
                var schools=data.extend;
                console.log(schools);
                makeSchoolSelection(data);
            }

        }
    });
    function makeSchoolSelection(data) {
        $("#school").empty();
        var option=$("<option></option>").append("请选择学校").val(0);
        option.appendTo("#school")
        $.each(data.extend.schools,function(index,item){
            var option=$("<option></option>").append(item.schoolname).val(item.schoolid);
            option.appendTo("#school");
        });
    }
    function getClass() {
        var option=$("#school option:selected");
        var schoolId=option.val();
        if(option.val()!==0){
            $.ajax({
                url:"../queryClassBySchoolId",
                type:"get",
                data:{'schoolId':schoolId},
                success:function (data) {
                    if(data.code===100){
                        console.log(data);
                        makeClassSelection(data);
                        
                    }
                }

            });
        }
    }
    function makeClassSelection(data) {
        $("#classes").empty();
        var option=$("<option></option>").append("请选择班级").val(0);
        option.appendTo("#classes")
        $.each(data.extend.classInfo,function(index,item){
            var option=$("<option></option>").append(item.classname).val(item.classid);
            option.appendTo("#classes");
        });
    }
    function getStudent() {
        var option=$("#classes option:selected");
        	classId=option.val();
        if(option.val()!==0){
            $.ajax({
                url:"../queryAllStudentWithClass",
                type:"get",
                data:{'classId':classId},
                success:function (data) {
                    if(data.code===100){
                        console.log(data);
                        makeStudentSelection(data.extend.pageInfo.list);
                        makePage(data);
                        build_page_nav(data);
                    }
                }

            });
        }
    }
    function makePage(result){
    	$("#page_Info").empty();
    	var center=$("<center></center>").append("当前第"+result.extend.pageInfo.pageNum+"页,总共"+result.extend.pageInfo.pages+
    			" 页,总"+result.extend.pageInfo.total+" 条数记录");
    	$("#page_Info").append(center);
    	
    }
    function build_page_nav(result){
    	$("#page_nav").empty();
    	var ul=$("<ul></ul>").addClass("pagination");
    	var firstPage=$("<li></li>").append($("<a></a>").append("首页").attr("href","#"));
    	var prePage=$("<li></li>").append($("<a></a>").append("&laquo;").attr("href","#"));
    	if(result.extend.pageInfo.hasPreviousPage==false){
    		firstPage.addClass("disabled");
    		prePage.addClass("disabled");
    	}else{
    		//为元素添加点击事件
    		firstPage.click(function(){
    			to_Page(1);
    		});
    		prePage.click(function(){
    			to_Page(result.extend.pageInfo.pageNum-1);
    		});
    	}
    	
    	
    	var nextPage=$("<li></li>").append($("<a></a>").append("&raquo;").attr("href","#"));
    	var lastPage=$("<li></li>").append($("<a></a>").append("末页").attr("href","#"));
    	if(result.extend.pageInfo.hasNextPage==false){
    		nextPage.addClass("disabled");
    		lastPage.addClass("disabled");
    	}else{
    		lastPage.click(function(){
    			to_Page(result.extend.pageInfo.pages);
    		});
    		nextPage.click(function(){
    			to_Page(result.extend.pageInfo.pageNum+1);
    		});
    	}
    	
    	
    	ul.append(firstPage).append(prePage);
    	$.each(result.extend.pageInfo.navigatepageNums,function(index,item){
    		var num=$("<li></li>").append($("<a></a>").append(item).attr("href","#"));
    		if(result.extend.pageInfo.pageNum==item){
    			num.addClass("active");
    		}
    		num.click(function(){
    			to_Page(item);
    		});
    		ul.append(num);
    	});
    	ul.append(nextPage).append(lastPage);
    	var nav=$("<nav></nav>").addClass("Page navigation").append(ul);
    	nav.appendTo("#page_nav");
    }
    function to_Page(pn){
    	$.ajax({
    		url:"../queryAllStudentWithClass",
    		data:{"classId":classId,"pn":pn},
    		type:"get",
    		success:function(result){
    			console.log(result);
    			makeStudentSelection(result.extend.pageInfo.list);
                makePage(result);
                build_page_nav(result);
    		}
    	})
    }
    //学生数据
    function makeStudentSelection(data) {
        var html=$('script[type="text/template"]').html();
        var arr=[];
        $("#student tbody").html("");
        $.each(data,function (i,o) {
        	arr.push(formatTemplate(o,html));
        })
        $("#student tbody").append(arr.join(""));
        selectAllOrNotAll();
    }
    function formatTemplate(user,tmpl) {
        return tmpl.replace(/{(\w+)}/g,function (m1,m2) {
            if(!m2){
                return "";
            }
            if(user[m2]===0||user[m2]===1){
                user[m2]=user[m2]===0?"女":"男";
            }
            return user[m2.toLowerCase()];
        })
    }

    /**
     * 选择与不全选
     */
    $("#selectAll").click(selectAllOrNotAll)
    function selectAllOrNotAll(){
        var selectAll=$("#selectAll").is(":checked");
        var checkBoxs=$("input[type='checkbox']");
        if(selectAll){
            $.each(checkBoxs,function () {
                $(this).prop("checked",true);
            })
        }else{
            $.each(checkBoxs,function () {
                $(this).prop("checked",false);
            })
        }
    }

    /**
     * 提交考核
     */
    $("#submit").click(submit);
    function submit() {
        var ruleId=getRule();
        var chekboxs=$("tbody").find(":checked");
        var studentNum=[];
        if(ruleId!==undefined&&chekboxs.length>0){
            for(var i=0;i<chekboxs.length;i++){
                var id=chekboxs.eq(i).val().split("_")[1];
                studentNum.push(id);
            }
            console.log(studentNum);
            var d=studentNum;
            $.ajax({
                url:"../updateStudent",
                type:"post",
                data:{"ruleId":ruleId,"studentNum":d.toString(),"classId":classId},
                success:function (data) {
                    if(data.code==100){
                        updataScore(studentNum,ruleId);
                        reminder("success","考勤成功");
                        $("#selectAll").prop("checked",false);
                        $.each(chekboxs,function(i){
                        	$(this).prop("checked",false);
                        })
                    }
                }
            })
        }
    }
    function updataScore(data,ruleId) {
        for(var i=0;i<data.length;i++){
            data[i]="#id"+data[i];
            console.log(data[i])
            var text=$(data[i]).html();
            var n=parseInt($("#"+ruleId).val());
            var num=parseInt(text);
            $(data[i]).html("");
            $(data[i]).html(num+n);
            console.log($(data[i])+":"+text)
        }
    }
    function getRule() {
        var option=$("#rule :selected");
        if(option.val()!==0){
            return option.attr("id");
        }
    }
  //提示信息
	function reminder(Status,msg) {
		swal({
			  title: msg,
			  text: "学生考勤系统",
			  timer: 1500,
			  type:Status,
			  showConfirmButton: false
			});
	}	
});
